package com.zz.yt.lib.bluetooth.delegate.print.gp.modle;


import androidx.annotation.NonNull;

import com.gprinter.command.EscCommand;
import com.gprinter.command.LabelCommand;
import com.whf.android.jar.util.log.LatteLogger;

import java.util.Vector;

/**
 * 打印内容
 */
public class PrintGpManage {

    /**
     * 默认每行的高度
     */
    private final int yAxis;

    /**
     * y轴
     */
    protected int yText = 6;

    /**
     * 打印的对象
     */
    protected LabelCommand tsc = null;

    @NonNull
    public static PrintGpManage create() {
        return new PrintGpManage(45);
    }

    @NonNull
    public static PrintGpManage create(int yAxis) {
        return new PrintGpManage(yAxis);
    }

    protected PrintGpManage(int yAxis) {
        this.yAxis = yAxis;
    }


    /**
     * 标签打印测试页
     */
    public Vector<Byte> getLabel() {
        yText = 6;
        initLabel();
        // 打印标题
        setText(220, "测试收费清单");
        //打印内容
        aline("单号", "211223001", "业主", "徐秀峰");
        aline("日期", "2021-12-23", "时间", "11:12:40");
        aline("收费", "测试用户", "房号", "1-1101");
        drawLine();

        alineCharge(true);
        alineCharge(false, "物业费22年1月", "93.7", "平方", "1.2", "112.44");
        alineCharge(false, "物业费22年2月", "93.7", "平方", "1.2", "543.21");
        alineCharge(false, "物业费22年3月", "93.7", "平方", "1.2", "123.45");
        alineCharge(false, "车位费22年1月", "1", "个", "30", "30");
        alineCharge(false, "车位费22年2月", "1", "个", "30", "30");
        alineCharge(false, "车位费22年3月", "1", "个", "30", "30");

        drawLine();

        endPrint(" 506.3", " 506.3", "测试物业公司", "测试小区", "监督电话: 023-4008008820");
        return getCommand();
    }

    /**
     * 初始化
     */
    protected void initLabel() {
        tsc = new LabelCommand();
        // 设置标签尺寸宽高，按照实际尺寸设置 单位mm
        tsc.addSize(550, 120);
        // 设置标签间隙，按照实际尺寸设置，如果为无间隙纸则设置为0 单位mm
        tsc.addGap(0);
        // 设置打印方向
        tsc.addDirection(LabelCommand.DIRECTION.FORWARD, LabelCommand.MIRROR.NORMAL);
        // 开启带Response的打印，用于连续打印
        tsc.addQueryPrinterStatus(LabelCommand.RESPONSE_MODE.ON);
        // 设置原点坐标
        tsc.addReference(0, 0);
        //设置浓度
        tsc.addDensity(LabelCommand.DENSITY.DNESITY4);
        // 撕纸模式开启
        tsc.addTear(EscCommand.ENABLE.ON);
        // 清除打印缓冲区
        tsc.addCls();
    }

    /**
     * 获得打印数据
     */
    protected Vector<Byte> getCommand() {
        // 打印标签
        tsc.addPrint(1, 1);
        // 打印标签后 蜂鸣器响
        tsc.addSound(2, 100);
        //开启线程
        tsc.addCashdrwer(LabelCommand.FOOT.F5, 255, 255);
        // 发送数据
        return tsc.getCommand();
    }

    /**
     * 每一个输入标签
     *
     * @param x:
     * @param text:绘制简体中文
     */
    protected void setText(int x, String text) {
        tsc.addText(x, yText, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0,
                LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, text);
    }

    /**
     * 每一行输入标签
     *
     * @param x:
     * @param text:绘制简体中文
     */
    protected void setAlineText(int x, String text) {
        yText += yAxis;
        setText(x, text);
    }

    /**
     * 基本信息
     *
     * @param paramString：
     */
    protected void aline(@NonNull String... paramString) {
        if (paramString.length > 3) {
            yText += yAxis;
            setText(20, paramString[0] + ": " + paramString[1]);
            setText(300, paramString[2] + ": " + paramString[3]);
        }
    }


    /**
     * 收费信息
     *
     * @param paramString：
     */
    protected void alineCharge(boolean is, @NonNull String... paramString) {
        yText += yAxis;
        if (!is && paramString.length < 4) {
            LatteLogger.e("收费信息有误");
            return;
        }
        if (is) {
            setText(20, "项目");
        } else {
            String string = paramString[0];
            if (string.length() > 10) {
                yText += 20;
                string = string.substring(0, 10) + "\n" + string.substring(10);
            }
            setText(20, string);
        }
        setText(270, is ? "数量" : paramString[1]);
        setText(350, is ? "单位" : paramString[2]);
        setText(430, is ? "单价" : paramString[3]);
        setText(510, is ? "金额" : paramString[4]);
    }

    /**
     * 信息尾页
     *
     * @param paramString：
     */
    protected void endPrint(@NonNull String... paramString) {
        if (paramString.length > 4) {
            yText += yAxis;
            setText(20, "应付金额 :");
            setText(380, paramString[0]);

            yText += yAxis;
            setText(20, "实付金额 :");
            setText(380, paramString[1]);

            yText += yAxis + 5;
            setText(150, "** " + paramString[2] + " **");
            yText += yAxis + 5;
            setText(200, "** " + paramString[3] + "  **");
            yText += yAxis + 5;
            setText(150, "[ " + paramString[4] + " ]");
        }
    }

    /**
     * 标签打印虚线
     */
    protected void drawLine() {
        yText += yAxis;
        String cont = "        ";
        tsc.add1DBarcode(20, yText, LabelCommand.BARCODETYPE.CODE128, 2, LabelCommand.READABEL.EANBEL, LabelCommand.ROTATION.ROTATION_0, cont);
        tsc.add1DBarcode(220, yText, LabelCommand.BARCODETYPE.CODE128, 2, LabelCommand.READABEL.EANBEL, LabelCommand.ROTATION.ROTATION_0, cont);
        tsc.add1DBarcode(420, yText, LabelCommand.BARCODETYPE.CODE128, 2, LabelCommand.READABEL.EANBEL, LabelCommand.ROTATION.ROTATION_0, cont);
    }

}
